home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / gui / eagui30.lha / EAGUI / Example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-01  |  11.6 KB  |  474 lines

  1. /*
  2.  * $RCSfile: Example.c,v $
  3.  *
  4.  * $Author: marcel $
  5.  *
  6.  * $Revision: 3.2 $
  7.  *
  8.  * $Date: 1994/12/01 07:04:30 $
  9.  *
  10.  * $Locker: marcel $
  11.  *
  12.  * $State: Exp $
  13.  *
  14.  * Description: This is an example of how to use EAGUI. In fact it is the complete version of
  15.  *     the example used in the tutorial[2]. It can be compiled under SAS/C 6.51. It should be
  16.  *     fairly trivial to modify this example to create any window you want[1]. Please note that
  17.  *     the contents of the gadgets aren't saved, so after a resize, everything is lost. Under
  18.  *     V39 it is very easy to get and set these attributes (with GT_GetGadgetAttrs() and
  19.  *     GT_SetGadgetAttrs()), and although it's a bit more difficult under V37, it can be done
  20.  *     there too (it's something you'll have to do anyway).
  21.  *
  22.  * [1] If you want to create a new window, it is enough to specify a new tree of objects. The
  23.  *     only other thing you might want to change is the fact that the window in this example
  24.  *     can only be resized in horizontal direction. If you change the last argument of the
  25.  *     WindowLimits() call to "~0" that's fixed too.
  26.  *
  27.  * [2] In fact, it is a slightly enhanced example, which shows a little bit more.
  28.  *
  29.  * Use a tab size of 5 to read this source! Comments were formatted for a right margin of 95,
  30.  * which matches my overscan and font settings. I hope it is readable for others.
  31.  */
  32.  
  33. /* standard headers */
  34. #include <stdlib.h>
  35.  
  36. #include <exec/types.h>
  37. #include <graphics/text.h>
  38. #include <intuition/intuition.h>
  39. #include <libraries/gadtools.h>
  40.  
  41. #include <clib/macros.h>
  42.  
  43. #include <proto/diskfont.h>
  44. #include <proto/exec.h>
  45. #include <proto/intuition.h>
  46. #include <proto/gadtools.h>
  47. #include <proto/graphics.h>
  48. #include <proto/dos.h>
  49.  
  50. /* EAGUI headers */
  51. #include "EAGUI.h"
  52. #include "EAGUI_pragmas.h"
  53.  
  54. /* Custom object(s) */
  55. #include "TextField.c"
  56.  
  57. /* globals */
  58. struct ea_Object *            winobj_ptr        = NULL;
  59. struct ea_Object *            okobj_ptr            = NULL;
  60. struct ea_Object *            cancelobj_ptr        = NULL;
  61. struct ea_Object *            hgroupobj_ptr        = NULL;
  62. struct Window *            win_ptr            = NULL;
  63. struct Screen *            scr_ptr            = NULL;
  64. struct Gadget *            gadlist_ptr        = NULL;
  65. struct Gadget *            stringgadget_ptr    = NULL;
  66. APTR                     visualinfo_ptr        = NULL;
  67. struct DrawInfo *            drawinfo_ptr        = NULL;
  68. struct TextFont *            tf_ptr            = NULL;
  69. struct Library *            EAGUIBase            = NULL;
  70. struct TextAttr             textattr             = {"helvetica.font", 15, FS_NORMAL, FPB_DISKFONT};
  71. struct Hook                relhook;
  72. struct Hook                tfminsizehook;
  73. struct Hook                tfrenderhook;
  74. struct IntuiMessage            imsg;
  75. struct ci_TextField            textfield1;
  76. STATIC UBYTE                rcs_id_string[]    = "$Id: Example.c,v 3.2 1994/12/01 07:04:30 marcel Exp marcel $";
  77.  
  78. /* prototypes */
  79. ULONG __saveds __asm        HookEntry(register __a0 struct Hook *, register __a2 VOID *, register __a1 VOID *);
  80. VOID                        InitHook(struct Hook *, ULONG (*)(), VOID *);
  81. ULONG                    rel_samesize(struct Hook *, struct List *, APTR);
  82. LONG                        init(VOID);
  83. VOID                        cleanup(STRPTR);
  84. VOID                        resizewindow(VOID);
  85. ULONG                    handlemsgs(VOID);
  86.  
  87. /* functions for hook handling */
  88. ULONG __saveds __asm HookEntry
  89. (
  90.     register __a0 struct Hook *    hook_ptr,
  91.     register __a2 VOID *        object_ptr,
  92.     register __a1 VOID *        message_ptr
  93. )
  94. {
  95.     return((*hook_ptr->h_SubEntry)(hook_ptr, object_ptr, message_ptr));
  96. }
  97.  
  98. VOID InitHook(struct Hook *h_ptr, ULONG (*func_ptr)(), VOID *data_ptr)
  99. {
  100.     if (h_ptr)
  101.     {
  102.         h_ptr->h_Entry = (ULONG (*)())HookEntry;
  103.         h_ptr->h_SubEntry = func_ptr;
  104.         h_ptr->h_Data = data_ptr;
  105.     }
  106. }
  107.  
  108. /* same size relation */
  109. ULONG rel_samesize(struct Hook *hook_ptr, struct List *list_ptr, APTR msg_ptr)
  110. {
  111.      struct ea_RelationObject *ro_ptr;
  112.      ULONG minx, miny;
  113.      ULONG x, y;
  114.      minx = 0;
  115.      miny = 0;
  116.  
  117.      /* examine the list of objects that are affected by the relation */
  118.      ro_ptr = (struct ea_RelationObject *)list_ptr->lh_Head;
  119.      while (ro_ptr->node.ln_Succ)
  120.      {
  121.           ea_GetAttrs(ro_ptr->object_ptr,
  122.                EA_MinWidth,        &x,
  123.                EA_MinHeight,       &y,
  124.                TAG_DONE);
  125.  
  126.           /* find the maximum values of the minimum sizes */
  127.           minx = MAX(x, minx);
  128.           miny = MAX(y, miny);
  129.  
  130.           ro_ptr = (struct ea_RelationObject *)ro_ptr->node.ln_Succ;
  131.      }
  132.  
  133.      /* set all objects to the newly found minimum sizes */
  134.      ro_ptr = (struct ea_RelationObject *)list_ptr->lh_Head;
  135.      while (ro_ptr->node.ln_Succ)
  136.      {
  137.           ea_SetAttrs(ro_ptr->object_ptr,
  138.                EA_MinWidth,        minx,
  139.                EA_MinHeight,       miny,
  140.                TAG_DONE);
  141.  
  142.           ro_ptr = (struct ea_RelationObject *)ro_ptr->node.ln_Succ;
  143.      }
  144.      return(0);
  145. }
  146.  
  147. /* initialize everything */
  148. LONG init(VOID)
  149. {
  150.     LONG w, h, bl, br, bt, bb;
  151.  
  152.     /* open the EAGUI library */
  153.     if (!(EAGUIBase = OpenLibrary(EAGUILIBRARYNAME, EAGUILIBRARYVERSION)))
  154.     {
  155.         cleanup("Couldn't open EAGUI.library.\n");
  156.     }
  157.  
  158.     /* open the font */
  159.     if (!(tf_ptr = OpenDiskFont(&textattr)))
  160.     {
  161.         cleanup("Couldn't open font.\n");
  162.     }
  163.  
  164.     /* initialize the relation */
  165.     InitHook(&relhook, rel_samesize, NULL);
  166.  
  167.     /* initialize textfield hooks */
  168.     InitHook(&tfminsizehook, meth_MinSize_TextField, NULL);
  169.     InitHook(&tfrenderhook, meth_Render_TextField, NULL);
  170.  
  171.     /* set up some defaults for all objects */
  172.     ea_SetAttr(NULL, EA_DefGTTextAttr, (ULONG)&textattr);
  173.  
  174.     /* now we can build the object tree */
  175.     if (!( winobj_ptr = VGroup
  176.         EA_BorderLeft,        4,
  177.         EA_BorderRight,    4,
  178.         EA_BorderTop,        4,
  179.         EA_BorderBottom,    4,
  180.         EA_Child,            ea_NewObject(EA_TYPE_CUSTOMIMAGE,
  181.             EA_BorderBottom,    4,
  182.             EA_MinSizeMethod,    &tfminsizehook,
  183.             EA_RenderMethod,    &tfrenderhook,
  184.             EA_UserData,        &textfield1,
  185.             TAG_DONE),
  186.         EA_Child,            GTString(NULL)
  187.             EA_InstanceAddress,    &stringgadget_ptr,
  188.             EA_MinWidth,        20,    /* Fixes a bug in the GadTools library, which
  189.                                  * renders the full contents of the gadget, if
  190.                                  * it is very small, and you're using a fixed-
  191.                                  * width font. Originally reported by Roy van
  192.                                  * der Woning.
  193.                                  */
  194.             End,
  195.         EA_Child,            hgroupobj_ptr = HGroup
  196.             EA_BorderTop,        4,
  197.             EA_Child,            okobj_ptr = GTButton("Ok")
  198.                 End,
  199.             EA_Child,            EmptyBox(1)
  200.                 End,
  201.             EA_Child,            cancelobj_ptr = GTButton("Cancel")
  202.                 End,
  203.             End,
  204.         End ) )
  205.     {
  206.         cleanup("Couldn't init the objects.\n");
  207.     }
  208.  
  209.      ea_NewRelation(hgroupobj_ptr, &relhook,
  210.           EA_Object,          okobj_ptr,
  211.           EA_Object,          cancelobj_ptr,
  212.           TAG_DONE);
  213.  
  214.     /* lock the screen */
  215.     if (!(scr_ptr = LockPubScreen(NULL)))
  216.     {
  217.         cleanup("Couldn't lock default public screen.\n");
  218.     }
  219.  
  220.     /* get VisualInfo and DrawInfo */
  221.     if (!(visualinfo_ptr = GetVisualInfo(scr_ptr, TAG_DONE)))
  222.     {
  223.         cleanup("Couldn't get the visual info.\n");
  224.     }
  225.     if (!(drawinfo_ptr = GetScreenDrawInfo(scr_ptr)))
  226.     {
  227.         cleanup("Couldn't get the draw info.\n");
  228.     }
  229.  
  230.     /* fill in the textfield structure */
  231.     textfield1.tf_string_ptr = "Enter a string here:";    /* title */
  232.     textfield1.tf_textattr_ptr = &textattr;                /* font */
  233.     textfield1.tf_flags = CITF_ALIGNTOP;                /* alignment flags */
  234.     textfield1.tf_frontpen = 2;                        /* frontpen color index */
  235.  
  236.      /* obtain the minimum dimensions of every object in the tree */
  237.      ea_GetMinSizes(winobj_ptr);
  238.  
  239.     /* get some attributes */
  240.     ea_GetAttrs(winobj_ptr,
  241.         EA_MinWidth,        &w,
  242.         EA_MinHeight,        &h,
  243.         EA_BorderLeft,        &bl,
  244.         EA_BorderRight,    &br,
  245.         EA_BorderTop,        &bt,
  246.         EA_BorderBottom,    &bb,
  247.         TAG_DONE);
  248.  
  249.      /* open the window */
  250.      if (!(win_ptr = OpenWindowTags(NULL,
  251.          WA_Title,            "EAGUI Example",
  252.         WA_Flags,            (WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SIZEGADGET | WFLG_SIZEBBOTTOM | WFLG_ACTIVATE),
  253.         WA_IDCMP,            (IDCMP_CLOSEWINDOW | BUTTONIDCMP | STRINGIDCMP | IDCMP_REFRESHWINDOW | IDCMP_NEWSIZE),
  254.         WA_InnerHeight,    h + bt + bb,
  255.         WA_InnerWidth,        (w + bl + br) * 2,
  256.         TAG_DONE)))
  257.     {
  258.         cleanup("Couldn't open the window.\n");
  259.     }
  260.  
  261.     /* set the window limits */
  262.     WindowLimits(
  263.         win_ptr,
  264.         w + win_ptr->BorderLeft + win_ptr->BorderRight + bl + br,
  265.         h + win_ptr->BorderTop + win_ptr->BorderBottom + bt + bb,
  266.         ~0,
  267.         h + win_ptr->BorderTop + win_ptr->BorderBottom + bt + bb);
  268.  
  269.     /* create the gadgets and add them to the window */
  270.     resizewindow();
  271.  
  272.     return(0);
  273. }
  274.  
  275. /* clean up everything that was created */
  276. VOID cleanup(STRPTR str_ptr)
  277. {
  278.     int rc = 0;
  279.  
  280.     /* if a string is passed, we assume there was some kind of error */
  281.     if (str_ptr)
  282.     {
  283.         Printf("Error: %s", str_ptr);
  284.         rc = 20;
  285.     }
  286.  
  287.     if (gadlist_ptr)
  288.     {
  289.         RemoveGList(win_ptr, gadlist_ptr, -1);
  290.         ea_FreeGadgetList(winobj_ptr, gadlist_ptr);
  291.         gadlist_ptr = NULL;
  292.     }
  293.  
  294.     if (win_ptr)
  295.     {
  296.         CloseWindow(win_ptr);
  297.         win_ptr = NULL;
  298.     }
  299.  
  300.     if (drawinfo_ptr)
  301.     {
  302.         FreeScreenDrawInfo(scr_ptr, drawinfo_ptr);
  303.         drawinfo_ptr = NULL;
  304.     }
  305.  
  306.     if (visualinfo_ptr)
  307.     {
  308.         FreeVisualInfo(visualinfo_ptr);
  309.         visualinfo_ptr = NULL;
  310.     }
  311.  
  312.     if (scr_ptr)
  313.     {
  314.         UnlockPubScreen(NULL, scr_ptr);
  315.         scr_ptr = NULL;
  316.     }
  317.  
  318.     if (winobj_ptr)
  319.     {
  320.         ea_DisposeObject(winobj_ptr);
  321.         winobj_ptr = NULL;
  322.     }
  323.  
  324.     if (tf_ptr)
  325.     {
  326.         CloseFont(tf_ptr);
  327.         tf_ptr = NULL;
  328.     }
  329.  
  330.     if (EAGUIBase)
  331.     {
  332.         CloseLibrary(EAGUIBase);
  333.         EAGUIBase = NULL;
  334.     }
  335.  
  336.     exit(rc);
  337. }
  338.  
  339. /* (re)create the gadget list */
  340. VOID resizewindow(VOID)
  341. {
  342.     LONG bl, br, bt, bb;
  343.  
  344.     /* if necessary, remove the gadget list from the window, and clean it up */
  345.     if (gadlist_ptr)
  346.     {
  347.         RemoveGList(win_ptr, gadlist_ptr, -1);
  348.         ea_FreeGadgetList(winobj_ptr, gadlist_ptr);
  349.         gadlist_ptr = NULL;
  350.     }
  351.  
  352.     ea_GetAttrs(winobj_ptr,
  353.         EA_BorderLeft,        &bl,
  354.         EA_BorderRight,    &br,
  355.         EA_BorderTop,        &bt,
  356.         EA_BorderBottom,    &bb,
  357.         TAG_DONE);
  358.  
  359.      ea_SetAttrs(winobj_ptr,
  360.         EA_Width,        win_ptr->Width -
  361.                     win_ptr->BorderLeft -
  362.                     win_ptr->BorderRight -
  363.                     bl -
  364.                     br,
  365.         EA_Height,    win_ptr->Height -
  366.                     win_ptr->BorderTop -
  367.                     win_ptr->BorderBottom -
  368.                     bt -
  369.                     bb,
  370.         EA_Left,        win_ptr->BorderLeft,
  371.         EA_Top,        win_ptr->BorderTop,
  372.         TAG_DONE);
  373.  
  374.     ea_LayoutObjects(winobj_ptr);
  375.  
  376.     if (ea_CreateGadgetList(winobj_ptr, &gadlist_ptr, visualinfo_ptr, drawinfo_ptr) != EA_ERROR_OK)
  377.     {     
  378.         cleanup("Couldn't create the gadget list.\n");
  379.     }     
  380.  
  381.     EraseRect(win_ptr->RPort,     
  382.         win_ptr->BorderLeft,
  383.         win_ptr->BorderTop,
  384.         win_ptr->Width - win_ptr->BorderRight - 1,
  385.         win_ptr->Height - win_ptr->BorderBottom - 1);
  386.  
  387.     RefreshWindowFrame(win_ptr);
  388.  
  389.     AddGList(win_ptr, gadlist_ptr, -1, -1, NULL);
  390.     RefreshGList(gadlist_ptr, win_ptr, NULL, -1);
  391.     GT_RefreshWindow(win_ptr, NULL);
  392.  
  393.     /* finally, we render the imagery, if there is any */
  394.     ea_RenderObjects(winobj_ptr, win_ptr->RPort);
  395. }
  396.  
  397. /* a normal message handling loop */
  398. ULONG handlemsgs(VOID)
  399. {
  400.     struct IntuiMessage    *    imsg_ptr;
  401.     ULONG                rc = 0;
  402.  
  403.     while (imsg_ptr = GT_GetIMsg(win_ptr->UserPort))
  404.     {
  405.         CopyMem((char *)imsg_ptr, (char *)&imsg, (long)sizeof(struct IntuiMessage));
  406.  
  407.         GT_ReplyIMsg(imsg_ptr);
  408.  
  409.         switch (imsg.Class)
  410.         {
  411.             case    IDCMP_REFRESHWINDOW:
  412.                 GT_BeginRefresh(win_ptr);
  413.                 GT_EndRefresh(win_ptr, TRUE);
  414.                 break;
  415.  
  416.             case    IDCMP_CLOSEWINDOW:
  417.                 rc = 10;
  418.                 break;
  419.  
  420.             case    IDCMP_NEWSIZE:
  421.                 resizewindow();
  422.                 /* Just for fun, we put a string in the string gadget after each
  423.                  * resize. This demonstrates how to use the EA_InstanceAddress
  424.                  * tag to obtain pointers to gadgets, which you can use to modify
  425.                  * the gadgets directly.
  426.                  */
  427.                 GT_SetGadgetAttrs(stringgadget_ptr, win_ptr, NULL,
  428.                     GTST_String,    "Ah, a size change! How nice.",
  429.                     TAG_DONE);
  430.                 break;
  431.         }
  432.     }
  433.     return(rc);
  434. }
  435.  
  436. /* main */
  437. int main(int argc, char *argv[])
  438. {
  439.     ULONG idcmpmask, signals;
  440.     BOOL done = FALSE;
  441.  
  442.     /* process any startup options the user has supplied */
  443.     if (argc > 1)
  444.     {
  445.         /* first argument is the font name */
  446.         textattr.ta_Name = argv[1];
  447.         if (argc > 2)
  448.         {
  449.             /* second argument is the font y-size */
  450.             textattr.ta_YSize = atoi(argv[2]);
  451.         }
  452.     }
  453.  
  454.     /* initialize everything */
  455.     init();
  456.  
  457.     /* event handling loop */
  458.     idcmpmask = 1L << win_ptr->UserPort->mp_SigBit;
  459.     while (done == FALSE)
  460.     {
  461.         signals = Wait(idcmpmask);
  462.         if (signals & idcmpmask)
  463.         {
  464.             if (handlemsgs() != 0)
  465.             {
  466.                 done = TRUE;
  467.             }
  468.         }
  469.     }
  470.  
  471.     /* cleanup everything */
  472.     cleanup(NULL);
  473. }
  474.